<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class PersonalZiliao extends IndexController
{
    public function index()
    {

        $article = Db::name('user')->where('id',input('session.user.id'))->order('id')->find();
        $this->assign('list', $article);

        return view('personal_ziliao/index');
    }

    public function address()
    {
        $upid = empty($_GET['upid'])?0:$_GET['upid'];

        $article = Db::name('address')->where('upid',$upid)->select();

        return json(['data'=>$article]);
    }


    public function edit()
    {
        if (request()->isPost()) {
            //获取地址信息
            $address = [
                'pro' => input('pro'),
                'city' => input('city'),
                'area' => input('area'),
                'street' => input('street')
            ];


            $pro = Db::name('address')->where('id',$address['pro'])->find();
            $city = Db::name('address')->where('id',$address['city'])->find();
            $area = Db::name('address')->where('id',$address['area'])->find();
            $street = Db::name('address')->where('id',$address['street'])->find();

            $province = $pro['name'].$city['name'].$area['name'].$street['name'];


            //更新数据
            $data = [
                'id' => input('session.user.id'),
                'nickname' => input('nickname'),
                'sex' => input('sex'),
                'birthday' => input('birthday'),
                'province' => $province,
                'work' => input('work'),
                'qq' => input('qq'),
                'sightml' => input('sightml')
            ];
            $validate = validate('User');
            if ($validate->check($data)) {
                if (Db::name('user')->where('id',$data['id'])->update(['nickname'=>$data['nickname'],'sex'=>$data['sex'],'birthday'=>$data['birthday'],'province'=>$data['province'],'work'=>$data['work'],'qq'=>$data['qq'],'sightml'=>$data['sightml']])) {
                    //动态
                    $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更新了个人资料'];
                    db('dongtai')->insert($dongtai);

                    return $this->success('保存信息成功');
                } else {
                    return $this->error('保存信息失败');
                }
            } else {
                return $this->error($validate->getError());
            }
            return;
        }
        return $this->fetch();
    }









    public function password()
    {	
        return view('personal_ziliao/password');
    }

    public function changePassword()
    {
        if (request()->isPost()) {
            if(Db::name('user')->where('id',input('session.user.id'))->update(['pwd'=>input('newPwd')])){
                //动态
                $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更改了密码'];
                db('dongtai')->insert($dongtai);

                return $this->success('保存信息成功');
            }else{
                return $this->error('保存信息失败');
            }
            
        }            
    }





    public function photo()
    {

        return view('personal_ziliao/photo');
    }

    public function uploadPhoto()
    {
        $file = request()->file('image');
        if(!$file){
            return $this->error('头像上传失败');
        }


        $info = $file->validate(['size'=>100000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            //上传成功
            if(Db::name('user')->where('id',input('session.user.id'))->update(['icon'=>$info->getSaveName()])){
                //动态
                $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '上传了新的头像'];
                db('dongtai')->insert($dongtai);

                return $this->success('头像上传成功');
            }else{
                return $this->error('头像上传失败');
            }
        }else{
            return $this->error($file->getError());
        }
    }



    public function mibao()
    {

        return view('personal_ziliao/mibao');
    }


    public function changemibao()
    {

        if (request()->isPost()) {
            $data = ['use_id' => input('session.user.id'), 'question' => input('question'), 'answer' => input('answer')];

            $validate = validate('Mibao');
            if ($validate->check($data)) {
                $result = Db::name('mibao')
                ->where('use_id',$data['use_id'])
                ->where('question',$data['question'])
                ->where('answer','not null')
                ->select();

                if($result){
                    if(Db::name('mibao')->where('use_id',$data['use_id'])->where('question',$data['question'])->update(['answer'=>$data['answer']])){
                        //动态
                        $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更改了密保问题'];
                        db('dongtai')->insert($dongtai);

                        return $this->success('保存信息成功');
                    }else{
                        return $this->error('保存信息失败');
                    }
                }else{
                    $num = db('mibao')->where('use_id',$data['use_id'])->select();
                    if(count($num)>=3){
                        return $this->error('密保问题不能超过3个');
                    }
                    if(db('mibao')->insert($data)){
                        //动态
                        $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '新增了密保问题'];
                        db('dongtai')->insert($dongtai);

                        return $this->success('保存信息成功');
                    }else{
                        return $this->error('保存信息失败');
                    }
                }
            }
            return $this->error($validate->getError());
        }
        return $this->fetch();
    }




    public function zhaopassword()
    {
        
        return view('personal_ziliao/zhaopassword');
    }

    public function dozhaopassword()
    {


        if (request()->isPost()) {
            $data = ['use_id' => input('session.user.id'), 'question' => input('question'), 'answer' => input('answer'), 'pwd' => input('pwd')];
            $result = Db::name('mibao')
                ->where('use_id',$data['use_id'])
                ->where('question',$data['question'])
                ->where('answer',$data['answer'])
                ->select();
            if($result){
                if(Db::name('user')->where('id',input('session.user.id'))->update(['pwd'=>$data['pwd']])){
                    //动态
                    $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更改了密码'];
                    db('dongtai')->insert($dongtai);

                    return $this->success('密码修改成功');
                }else{
                    return $this->error('请更换新的密码');
                }
            }else{
                return $this->error('密保信息不正确');
            }   
            
        }
        return $this->fetch();
    }





    public function changeemail()
    {
        return view('personal_ziliao/email');
    }


    public function sendemail()
    {
        $userList = db('user')->where('id',input('session.user.id'))->find();
        $this->assign('userList', $userList);
        if (request()->isPost()) {
            $request = request()->root(true);

            $email = input('email');
            $parm1 = base64_encode('email');
            $parm2 = base64_encode($email);

            $mail = new PHPMailer();
            $mail->CharSet = "UTF-8";
            $address ="1475737115@qq.com";
            $mail->IsSMTP(); // 使用SMTP方式发送
            $mail->Host = "smtp.qq.com"; // 您的企业邮局域名
            $mail->SMTPAuth = true; // 启用SMTP验证功能
            $mail->Username = "1475737115@qq.com"; // 邮局用户名(请填写完整的email地址)
            $mail->Password = "zscxguhvhftzfjgc"; // 邮局密码
            $mail->From = "1475737115@qq.com"; //邮件发送者email地址
            $mail->FromName = "波奇论坛";
            $mail->AddAddress($email, $userList['nickname']);//收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
            //$mail->AddReplyTo("", "");

            //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
            $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

            $mail->Subject = "波奇论坛邮箱验证提醒"; //邮件标题
            $mail->Body = "您刚才在 波奇论坛 申请了邮箱验证，请点击下面的链接完成验证：".$request.url('index/PersonalZiliao/dochangeemail');

            if(!$mail->Send())
            {
                return $this->error('邮件发送失败:'.$mail->ErrorInfo);

            }else{
                db('user')->where('id',input('session.user.id'))->update(['email' => $email,'email_status' => '0']);

                $this->assign('email', $email);
                return view('personal_ziliao/email2');

            }


        }
        return view('personal_ziliao/changeemail');

    }



    public function dochangeemail()
    {
        db('user')->where('id',input('session.user.id'))->update(['email_status' => '1']);
        //动态
        $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更换了绑定邮箱'];
        db('dongtai')->insert($dongtai);

        return view('personal_ziliao/email3');
    }


}